package com.etc.controller;

import com.etc.common.Constants;
import com.etc.common.Order;
import com.etc.common.TrackPoint;
import com.etc.modules.ResultModel;
import com.etc.util.HbaseUtil;
import com.etc.util.JedisUtil;
import com.etc.util.ObjUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * @Author kalista
 * @Description 轨迹控制器
 * @Date 2021/5/30  15:41
 **/
@RestController
@RequestMapping("/track")
public class TrackMonitorController {




    /**
     * 根据订单id查询轨迹点
     * @param orderId
     * @return
     */
    @GetMapping("/historyTrackPoints")
    public ResultModel<List<TrackPoint>> history(String orderId) throws Exception {
        ResultModel<List<TrackPoint>> resultModel = new ResultModel<List<TrackPoint>>();
        // TODO 调用 hbaseutils 得到轨迹图
        JedisUtil instance = JedisUtil.getInstance();
        Jedis jedis = instance.getJedis();
        Object obj = null;
        byte[] orderBytes = jedis.hget(Constants.ORDER_START_ENT_TIME.getBytes(), orderId.getBytes());
        if (orderBytes != null){
            obj = ObjUtil.deserialize(orderBytes);
        }
        if (obj != null){
            Order order = (Order)obj;
            // 开始时间
            String startTime = order.getStartTime()+"";
            // 结束时间
            String entTime = order.getEndTime()+"";
            // 表名
            String tableName = Constants.HTAB_GPS;
            List<TrackPoint> rest = HbaseUtil.getRest(tableName, order.getOrderId(), startTime, entTime, TrackPoint.class);
            resultModel.setData(rest);
            resultModel.setSuccess(true);
        }
        return resultModel;
    }

}
